package com.example.WebSocketDemo.web;

import com.example.WebSocketDemo.bean.Message;
import com.example.WebSocketDemo.bean.User;
import com.example.WebSocketDemo.service.MessageService;
import com.example.WebSocketDemo.service.UserService;
import com.example.WebSocketDemo.util.Result;
import com.example.WebSocketDemo.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private MessageService messageService;

    @PostMapping("/login")
    public Result login(@RequestBody User user)
    {
        System.out.println(user);
        User u = userService.login(user);
        if(u!=null){
            return Result.success(u);
        }
        else {
            return Result.failure(ResultCode.USER_LOGIN_ERROR);
        }

    }
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        //System.out.println(user);
        userService.userRegister(user);
        return Result.success();
    }
    @GetMapping("/getAll")
    public Result findAll(){
        return Result.success(userService.getAllUser());
    }

    @GetMapping("/friend/{userId}")
    public Result getUserFriend(@PathVariable String userId){
        return Result.success(userService.getUserFriend(userId));
    }

    @GetMapping("/info/{userId}")
    public Result getUserInfoById(@PathVariable String userId){
        return Result.success(userService.getUserInfoById(userId));
    }

    @GetMapping("/get/{info}")
    public Result searchUser(@PathVariable String info){
        return Result.success(userService.searchUser(info));
    }

    @PostMapping("/update")
    public Result searchUser(@RequestBody User user){
       // System.out.println(user);
        userService.modifyUserInfo(user);
        return Result.success();
    }


}
